package org.csu.jpetmanage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import jakarta.servlet.http.HttpSession;
import org.csu.jpetmanage.mapper.SupplierMapper;
import org.csu.jpetmanage.model.Supplier;
import org.csu.jpetmanage.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
//@RequestMapping(value = "/Item")
public class SupplierController {
    @Autowired
    SupplierService supplierService;

    @Autowired
    Supplier supplier;

    @Autowired
    SupplierMapper supplierMapper;

    String message = null;

    @GetMapping("/displaySupplier")
    public String displaySupplier( Model model){
        List<Supplier> list =supplierService.list();
        model.addAttribute("supplierList" ,list);

        return "yuan/supplier";
    }



    @RequestMapping("/addSupplier")
    public String addSupplier(
            @RequestParam("suppid") String suppid,
            @RequestParam("name") String name,
            @RequestParam("status") String status,
            @RequestParam("addr1") String addr1,
            @RequestParam("addr2") String addr2,
            @RequestParam("city") String city,
            @RequestParam("state") String state,
            @RequestParam("zip") String zip,
            @RequestParam("phone") String phone,
            Model model,
            HttpSession session){
        List<Supplier> list =supplierService.list();
        model.addAttribute("supplierList" ,list);
        supplier.setSuppid(suppid);
        supplier.setName(name);
        supplier.setStatus(status);
        supplier.setAddr1(addr1);
        supplier.setAddr2(addr2);
        supplier.setCity(city);
        supplier.setState(state);
        supplier.setZip(zip);
        supplier.setPhone(phone);
        supplierMapper.insert(supplier);
        session.setAttribute("msg","插入成功");
        return "redirect:/displaySupplier";
    }

    @RequestMapping("/deleteSupplier")
    public String deleteItem(
            @RequestParam("suppid") String suppid,
            Model model,
            HttpSession session
    ){
        List<Supplier> list =supplierService.list();
        model.addAttribute("supplierList" ,list);
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("suppid",suppid);
        int sum = supplierMapper.deleteByMap(hashMap);
        if (sum>=1){
            session.setAttribute("msg","删除成功");
        }
        return "redirect:/displaySupplier";
    }

    @GetMapping("/supplierEditAjax")
    @ResponseBody
    public Map<String,Object> supplierEditAjax(
            @RequestParam("suppid") String suppid,
            HttpSession session
    ){
        QueryWrapper<Supplier> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("suppid",suppid);
        List<Supplier> supplierList = supplierMapper.selectList(queryWrapper);
        Map<String, Object> map = new HashMap<>();
        if (supplierList.size()==1){
            supplier = supplierList.get(0);
            session.setAttribute("updateSupplier",supplier);//待会处理编辑数据的时候需要用
            //把下面补全
            map.put("suppid",supplier.getSuppid());
            map.put("name",supplier.getName());
            map.put("status",supplier.getStatus());
            map.put("addr1",supplier.getAddr1());
            map.put("addr2",supplier.getAddr2());
            map.put("city",supplier.getCity());
            map.put("state",supplier.getState());
            map.put("zip",supplier.getZip());
            map.put("phone",supplier.getPhone());
            return map;
        }
        return map;
    }

    @RequestMapping("/updateSupplier")
    public String updateSupplier(
            @RequestParam("updatesuppid") String suppid,
            @RequestParam("updatename") String name,
            @RequestParam("updatestatus") String status,
            @RequestParam("updateaddr1") String addr1,
            @RequestParam("updateaddr2") String addr2,
            @RequestParam("updatecity") String city,
            @RequestParam("updatestate") String state,
            @RequestParam("updatezip") String zip,
            @RequestParam("updatephone") String phone,
            Model model,
            HttpSession session) {
        supplier = (Supplier)session.getAttribute("updateSupplier");
        supplier.setSuppid(suppid);
        supplier.setName(name);
        supplier.setStatus(status);
        supplier.setAddr1(addr1);
        supplier.setAddr2(addr2);
        supplier.setCity(city);
        supplier.setState(state);
        supplier.setZip(zip);
        supplier.setPhone(phone);
        UpdateWrapper<Supplier> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("suppid",supplier.getSuppid());
        int rows = supplierMapper.update(supplier,updateWrapper);
        session.setAttribute("msg","修改成功");
        return "redirect:/displaySupplier";
    }

    @RequestMapping("/searchSupplier")
    public String searchSupplier(
            @RequestParam("keyword") String keyword,
            Model model){
        QueryWrapper<Supplier> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("suppid",keyword).
                or().like("name",keyword).
                or().like("status",keyword).
                or().like("addr1",keyword).
                or().like("addr2",keyword).
                or().like("city",keyword).
                or().like("state",keyword).
                or().like("zip",keyword).
                or().like("phone",keyword);
        List<Supplier> supplierList=supplierMapper.selectList(queryWrapper);
        model.addAttribute("supplierList" ,supplierList);
        return "yuan/supplier";
    }

    @GetMapping("/supplierSearchAjax")
    @ResponseBody
    public List<Supplier> supplierSearchAjax(
            @RequestParam("keyword") String keyword,
            HttpSession session){
        QueryWrapper<Supplier> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("suppid",keyword).
                or().like("name",keyword).
                or().like("status",keyword).
                or().like("addr1",keyword).
                or().like("addr2",keyword).
                or().like("city",keyword).
                or().like("state",keyword).
                or().like("zip",keyword).
                or().like("phone",keyword);
        return supplierMapper.selectList(queryWrapper);
    }
}
